package repository

import (
	"go-iot-project/apps/entity"
	"go-iot-project/pkg/db"
)

type (
	DeviceModel interface {
		Insert(data *entity.Device) *entity.Device
		FindOnyByDeviceNo(deviceId string) (*entity.Device, error)
		UpateDevice(device *entity.Device) error
	}
	// 设备实现
	DeviceImpl struct{}
)

// FindOnyByDeviceNo 获取设备
func (d *DeviceImpl) FindOnyByDeviceNo(deviceId string) (*entity.Device, error) {
	var device *entity.Device
	if err := db.MyDb.Where("device_id = ?", deviceId).First(device).Error; err != nil {
		return device, err
	}
	return device, nil
}

// Add 添加一个设备
func (d *DeviceImpl) Insert(data *entity.Device) *entity.Device {
	if err := db.MyDb.Create(data).Error; err != nil {
		return nil
	}
	return data
}

// UpdateDevice 更新设备
func (d *DeviceImpl) UpdateDevice(device *entity.Device) error {
	if err := db.MyDb.Updates(device).Error; err != nil {
		return err
	}
	return nil
}
